package CRUD;

// default package
// Generated Apr 21, 2011 3:13:26 PM by Hibernate Tools 3.3.0.GA

import Utility.SessionFactoryUtil;
import java.util.List;
import javax.naming.InitialContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;
import xmlMapping.Message;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
/**
 * Home object for domain model class Message.
 * @see .Message
 * @author Hibernate Tools
 */
public class MessageHome {

	private static final Log log = LogFactory.getLog(MessageHome.class);


      	public Session session;

        public MessageHome() {
               if(session==null|| !session.isOpen())
           
        session=SessionFactoryUtil.getSessionFactory().openSession();
       
      }

	public void persist(Message transientInstance) {
		log.debug("persisting Message instance");
            
                session.beginTransaction();
		try {
		
                
                    session.persist(transientInstance);
                     session.getTransaction().commit();
			log.debug("persist successful");
                       
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void attachDirty(Message instance) {
		log.debug("attaching dirty Message instance");
            
                session.beginTransaction();
		try {
			session.saveOrUpdate(instance);
                         session.getTransaction().commit();
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Message instance) {
		log.debug("attaching clean Message instance");
            
                session.beginTransaction();
		try {
			session.lock(instance, LockMode.NONE);
                         session.getTransaction().commit();
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void delete(Message persistentInstance) {
		log.debug("deleting Message instance");
            
                session.beginTransaction();
		try {
			session.delete(persistentInstance);
                         session.getTransaction().commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Message merge(Message detachedInstance) {
		log.debug("merging Message instance");
            
                session.beginTransaction();
	try {
			Message result = (Message) session.merge(detachedInstance);
                         session.getTransaction().commit();
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
		throw re;
		}
	}

	public Message findById(java.lang.Integer id) {
        
                session.beginTransaction();
		log.debug("getting Message instance with id: " + id);
	try {
		Message instance = (Message) session.get(Message.class, id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
		return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Message instance) {
        
                session.beginTransaction();
		log.debug("finding Message instance by example");
		try {
			   Criteria crit=session.createCriteria(Message.class);
        
        
        Criterion cr=Restrictions.eq("memberByMemberId", instance.getMemberByMemberId());
       // Criterion opened=Restrictions.eq("opened", false);
        crit.add(cr);
crit.addOrder(Order.asc("date")) ;
        
       // crit.add(opened);
             List results=crit.list();
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
}
